package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrGroupRespVo;
import com.atguigu.gulimall.product.vo.AttrGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

/**
 * 属性分组
 *
 * @author zengjinjin
 * @email 634261259@gmail.com
 * @date 2021-11-06 13:19:04
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{categoryId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable String categoryId) {
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, categoryId);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] catelogPath = attrGroupService.findCatelogPathById(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 获取属性分组的关联的所有属性
     */
    @GetMapping("/{attrGroupId}/attr/relation")
    public R getAttrRelation(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrEntity> attrEntityList = attrService.getAttrRelation(attrGroupId);
        return R.ok().put("data", attrEntityList);
    }

    /**
     * 获取属性分组的未关联的所有属性
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getNoAttrRelation(@RequestParam Map<String, Object> params, @PathVariable("attrgroupId") Long attrGroupId) {
        PageUtils page = attrService.getNoAttrRelation(params, attrGroupId);
        return R.ok().put("page", page);
    }

    /**
     * 删除属性与分组的关联关系
     */
    @RequestMapping("/attr/relation/delete")
    public R deleteAttrRelation(@RequestBody List<AttrGroupVo> attrGroupVo) {
        attrGroupService.deleteAttrRelation(attrGroupVo);
        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/attr/relation")
    public R save(@RequestBody List<AttrGroupVo> attrGroupVoList) {
        attrAttrgroupRelationService.saveBatch(attrGroupVoList);

        return R.ok();
    }

    /**
     * 获取分类下的所有属性分组信息
     */
    @GetMapping("/{catelogId}/withattr")
    public R getWithAttrByCatelogId(@PathVariable("catelogId") Long catelogId) {

        List<AttrGroupRespVo> attrGroupRespList = attrGroupService.findAttrGroupByCatId(catelogId);
        return R.ok().put("data", attrGroupRespList);
    }
}
